package org.jsets.fastboot.database;

/**
 * TODO
 *
 * @author wangjie (https://github.com/wj596)
 * @date 2022.01.06 17:18
 * @since 0.1
 */
public abstract class MySQLTypes {

    public static final String VARCHAR = "VARCHAR";// 	java.lang.String 	12
    public static final String CHAR 	= "CHAR";// 	java.lang.String 	1
    public static final String BLOB 	= "BLOB";// 	java.lang.byte[] 	-4
    public static final String TINYBLOB 	= "TINYBLOB";// 	java.lang.byte[] 	-4
    public static final String TEXT 	= "TEXT";// 	java.lang.String 	-1
    public static final String INTEGER 	= "INTEGER";// 	java.lang.Long 	4
    public static final String INTEGER_UNSIGNED 	= "INTEGER UNSIGNED";// 	java.lang.Long 	4
    public static final String TINYINT = "TINYINT";//  	java.lang.Integer 	-6
    public static final String TINYINT_UNSIGNED 	= "TINYINT UNSIGNED";// 	java.lang.Long 	4
    public static final String SMALLINT 	= "SMALLINT";// UNSIGNED 	java.lang.Integer 	5
    public static final String MEDIUMINT 	= "MEDIUMINT";// UNSIGNED 	java.lang.Integer 	4
    public static final String BIT 	= "BIT";// 	java.lang.Boolean 	-7
    public static final String BIGINT 	= "BIGINT";//  	java.math.BigInteger 	-5
    public static final String BIGINT_UNSIGNED 	= "BIGINT UNSIGNED";// 	java.math.BigInteger 	-5
    public static final String FLOAT 	= "FLOAT";// 	java.lang.Float 	7
    public static final String FLOAT_UNSIGNED 	= "FLOAT UNSIGNED";// 	java.lang.Float 	7
    public static final String DOUBLE 	= "DOUBLE";// 	java.lang.Double 	8
    public static final String DOUBLE_UNSIGNED 	= "DOUBLE UNSIGNED";// 	java.lang.Double 	8
    public static final String DECIMAL 	= "DECIMAL";// 	java.math.BigDecimal 	3
    public static final String DECIMAL_UNSIGNED 	= "DECIMAL UNSIGNED";// 	java.math.BigDecimal 	3
    public static final String BOOLEAN 	= "TINYINT";//
    public static final String DATE 	= "DATE";// 	java.sql.Date 	91
    public static final String TIME 	= "TIME";// 	java.sql.Time 	92
    public static final String DATETIME = "DATETIME";// 	java.sql.Timestamp 	93
    public static final String TIMESTAMP = "TIMESTAMP";// 	java.sql.Timestamp 	93
    public static final String YEAR 	= "YEAR";// 	java.sql.Date

    
	/**
	 * java类型转MySQL类型
	 * 
	 * @param javaType Java类型
	 * @return
	 */
	public static String fromJavaType(String javaType) {
		switch (javaType) {
		case JavaTypes.STRING:
			return MySQLTypes.VARCHAR;
		case JavaTypes.DATE:
			return MySQLTypes.DATE;
		case JavaTypes.TIMESTAMP:
			return MySQLTypes.TIMESTAMP;
		case JavaTypes.INTEGER:
			return MySQLTypes.INTEGER;
		case JavaTypes.LONG:
			return MySQLTypes.BIGINT;
		case JavaTypes.FLOAT:
			return MySQLTypes.FLOAT;
		case JavaTypes.DOUBLE:
			return MySQLTypes.DOUBLE;
		case JavaTypes.BOOLEAN:
			return MySQLTypes.TINYINT;
		case JavaTypes.BIG_DECIMAL:
			return MySQLTypes.DECIMAL;
		case JavaTypes.BYTES:
			return MySQLTypes.BLOB;
		}
		return MySQLTypes.VARCHAR;
	}

	public static String toJavaType(String mysqlType) {
		String upper = mysqlType.toUpperCase();
		switch (upper) {
		case MySQLTypes.VARCHAR:
		case MySQLTypes.CHAR:
		case MySQLTypes.TEXT:
			return JavaTypes.STRING;

		case MySQLTypes.DATE:
		case MySQLTypes.DATETIME:
		case MySQLTypes.TIMESTAMP:
		case MySQLTypes.TIME:
		case MySQLTypes.YEAR:
			return JavaTypes.DATE;

		case MySQLTypes.BIT:
		case MySQLTypes.INTEGER:
		case MySQLTypes.TINYINT:
		case MySQLTypes.SMALLINT:
		case MySQLTypes.INTEGER_UNSIGNED:
		case MySQLTypes.TINYINT_UNSIGNED:
			return JavaTypes.INTEGER;

		case MySQLTypes.BIGINT:
		case MySQLTypes.BIGINT_UNSIGNED:
			return JavaTypes.LONG;

		case MySQLTypes.FLOAT:
		case MySQLTypes.FLOAT_UNSIGNED:
			return JavaTypes.FLOAT;

		case MySQLTypes.DOUBLE:
		case MySQLTypes.DOUBLE_UNSIGNED:
			return JavaTypes.DOUBLE;

		case MySQLTypes.DECIMAL:
		case MySQLTypes.DECIMAL_UNSIGNED:
			return JavaTypes.BIG_DECIMAL;

		case MySQLTypes.BLOB:
		case MySQLTypes.TINYBLOB:
			return JavaTypes.BYTES;

		}
		return JavaTypes.STRING;
	}

}